package com.shanks.home.net;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class SocketInputStream {
	private boolean 		m_bBigEndian = true;
	private DataInputStream	m_in = null;
	
	public SocketInputStream(InputStream in){
		m_in = new DataInputStream(in);
		m_bBigEndian = true;		
	}
	
	public SocketInputStream(InputStream in, boolean bBigEndian){
		m_in = new DataInputStream(in);
		m_bBigEndian = bBigEndian;
	}

	public byte readByte() throws IOException{
		return m_in.readByte();
	}
	
	public short readShort() throws IOException{
		if (m_bBigEndian)
			return m_in.readShort();
		
		return Short.reverseBytes(m_in.readShort());
	}
	
	public int readInt() throws IOException{
		if (m_bBigEndian)
			return m_in.readInt();
		
		return Integer.reverseBytes(m_in.readInt());
	}
	
	public byte[] readBuffer(int len) throws IOException{
		if (len <= 0)
			return null;
		
		byte[] result = new byte[len];
		m_in.read(result);
		
		return result;
	}
	
	public String readString() throws IOException {
		int len = readInt();
		return new String(readBuffer(len), "utf-8");
	}

	
	public long readLong() throws IOException{
		if (m_bBigEndian)
			return m_in.readLong();
		
		return Long.reverseBytes(m_in.readLong());
	}
	
	public void read(byte[] buffer) throws IOException{
		m_in.read(buffer);
	}
	
	public void close() throws IOException{
		m_in.close();
	}
}
